package com.zhku.zhny.admin.web.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import com.alibaba.fastjson.JSON;
import com.zhku.zhny.admin.constant.ConstantFilePath;
import com.zhku.zhny.admin.exception.RemoveException;
import com.zhku.zhny.admin.exception.SearchException;
import com.zhku.zhny.admin.service.AdminService;
import com.zhku.zhny.admin.service.AdminServiceImpl;
import com.zhku.zhny.domain.Honor;
import com.zhku.zhny.domain.HonorPageBean;
import com.zhku.zhny.util.BaseServlet;
import com.zhku.zhny.util.FileOperationUtil;
import com.zhku.zhny.util.RandomIdFactory;

@WebServlet("/info/honor")
public class HonorInfoServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addHonor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, String[]> map = FileOperationUtil.upload(request, ConstantFilePath.LOCALHONORFILEPATH);
			Honor honor = new Honor();
			honor.setHonorId(RandomIdFactory.getRandomId());
			ConvertUtils.register(new DateLocaleConverter(),Date.class);// 将字符串日期转换为日期类型
			BeanUtils.populate(honor, map);
			AdminService adminService = new AdminServiceImpl();
			int i = adminService.addHonorInfo(honor);
			if (i > 0) {
				return "redirect:/admin/success.html";
			}else {
				return "redirect:/admin/fail.html";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/admin/fail.html";
		}
	}
	
	/**
	 * 删除单个
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void removeHonor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String honorId = request.getParameter("honorId");
		AdminService adminService = new AdminServiceImpl();
		boolean result = false;
		try {
			result = adminService.removeHonorById(honorId);
			response.getWriter().print(result);
		} catch (RemoveException e) {
			e.printStackTrace();
			response.getWriter().print(result);
		}
	}
	
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void removeHonors(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		AdminService adminService = new AdminServiceImpl();
		boolean result = false;
		try {
			result = adminService.removeHonorByIds(ids);
			if(result) {
				response.getWriter().print(result);
			}else {
				response.getWriter().print(result);
			}
		} catch (RemoveException e) {
			e.printStackTrace();
			response.getWriter().print(result);
		}
	}
	
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void modifyHonor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void fySearchHonors(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String date = request.getParameter("date");
		String currentPage = request.getParameter("currentPage");
		int pageSize = 10;// 设置每页显示记录数
		//调用业务逻辑
		AdminService adminService = new AdminServiceImpl();
		HonorPageBean bean = new HonorPageBean();
		try {
			bean = adminService.findHonorByNameOrDate(name, date, currentPage, pageSize);
			String data = JSON.toJSONString(bean);// 将数据封装为json对象
//			response.setContentType("text/html;charset=UTF-8");
			// 响应回客户端
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}
	
	/**
	 * 查询出所有荣誉信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void searchHonors(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminService adminService = new AdminServiceImpl();
		try {
			List<Honor>honors = adminService.findAllHonor();
			Honor honor = null;
			for (int i = 0; i < honors.size(); i++) {
				honor = honors.get(i);
				// 修改图片访问路径
				String newPath = ConstantFilePath.HONORDIRECTORY+honor.getHonorImgPath();
				honor.setHonorImgPath(newPath);
			}
			String data = JSON.toJSONString(honors);
//			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}

}
